package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwLyqsclqksmDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwLyqsclqksm;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwLyqsclqksmService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwLyqsclqksmForm;


import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwCltfcsqkDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwCltfcsqk;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwCltfcsqkService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwCltfcsqkForm;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwCltfcsqkServiceImpl  implements IYwHwCltfcsqkService{
	@Autowired
	private YwHwCltfcsqkDao ywHwCltfcsqkDao;
	
	/**
	 * 获取form
	 */
	public YwHwCltfcsqkForm getForm(Long Id) {
		YwHwCltfcsqk entity=ywHwCltfcsqkDao.get(Id);
		YwHwCltfcsqkForm form=new DozerBeanMapper().map(entity,YwHwCltfcsqkForm.class);
		return form;
	}

	public List<YwHwCltfcsqkForm> getListByQyId(Long qyId) {
		List<YwHwCltfcsqk> list=this.getListByQyid(qyId);
		List<YwHwCltfcsqkForm> forms=new ArrayList<YwHwCltfcsqkForm>();
		for(YwHwCltfcsqk entity:list){
			YwHwCltfcsqkForm form=new DozerBeanMapper().map(entity,YwHwCltfcsqkForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据ywid查询数据
	 * @param QYId
	 * @return
	 */
	private List<YwHwCltfcsqk> getListByQyid(Long qyId){
		String sql=" from "+YwHwCltfcsqk.class.getName()+" where  qyid="+qyId;
		List<YwHwCltfcsqk> list=this.ywHwCltfcsqkDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwCltfcsqkForm form) {
		YwHwCltfcsqk entity=new DozerBeanMapper().map(form,YwHwCltfcsqk.class);
		this.ywHwCltfcsqkDao.save(entity);
	}

	public void SaveOrUpdate(YwHwCltfcsqk entity) {
		this.ywHwCltfcsqkDao.save(entity);
	}

	public void parseYwHwCltfcsqk(HSSFSheet hssfsheet,Long qybh,Long ywid, String templateCode){
		String hql = "from YwHwCltfcsqk t where t.ywid = '"+ywid+"' and t.tplcode= '"+templateCode+"'";
		List<YwHwCltfcsqk> list = new ArrayList<YwHwCltfcsqk>();
		list = this.ywHwCltfcsqkDao.find(hql, list);
		for(int i=0; i<list.size();i++){
			this.ywHwCltfcsqkDao.delete(list.get(i));
		}
		try {
			String bgdd;
			String bgcdmj;
			String cltfdd;
			String tfcdmj;
			String bgdxz;
			String cltfdxz;
			String ssqksm;
			String azjly;
			String sjgsqksm;
			String mbqk;
			
			HSSFRow hssfrow = hssfsheet.getRow(1);
			if(hssfrow.getCell((short) 1) == null){
				bgdd = "";
			} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
				bgdd = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
			}else{
				bgdd = hssfrow.getCell((short) 1).getStringCellValue().trim();
			}
			if(hssfrow.getCell((short) 3) == null){
				bgcdmj = "";
			} else if (hssfrow.getCell((short) 3).getCellType() == 0) {
				bgcdmj = new Double(hssfrow.getCell((short) 3).getNumericCellValue()).toString();
			}else{
				bgcdmj = hssfrow.getCell((short) 3).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(2);
			if(hssfrow.getCell((short) 1) == null){
				cltfdd = "";
			} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
				cltfdd = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
			}else{
				cltfdd = hssfrow.getCell((short) 1).getStringCellValue().trim();
			}
			if(hssfrow.getCell((short) 3) == null){
				tfcdmj = "";
			} else if (hssfrow.getCell((short) 3).getCellType() == 0) {
				tfcdmj = new Double(hssfrow.getCell((short) 3).getNumericCellValue()).toString();
			}else{
				tfcdmj = hssfrow.getCell((short) 3).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(3);
			if(hssfrow.getCell((short) 1) == null){
				bgdxz = "";
			} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
				bgdxz = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
			}else{
				bgdxz = hssfrow.getCell((short) 1).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(4);
			if(hssfrow.getCell((short) 1) == null){
				cltfdxz = "";
			} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
				cltfdxz = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
			}else{
				cltfdxz = hssfrow.getCell((short) 1).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(7);
			if(hssfrow.getCell((short) 0) == null){
				ssqksm = "";
			} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
				ssqksm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
			}else{
				ssqksm = hssfrow.getCell((short) 0).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(9);
			if(hssfrow.getCell((short) 1) == null){
				azjly = "";
			} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
				azjly = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
			}else{
				azjly = hssfrow.getCell((short) 1).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(11);
			if(hssfrow.getCell((short) 0) == null){
				sjgsqksm = "";
			} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
				sjgsqksm = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
			}else{
				sjgsqksm = hssfrow.getCell((short) 0).getStringCellValue().trim();
			}
			hssfrow = hssfsheet.getRow(13);
			if(hssfrow.getCell((short) 1) == null){
				mbqk = "";
			} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
				mbqk = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
			}else{
				mbqk = hssfrow.getCell((short) 1).getStringCellValue().trim();
			}
			

			YwHwCltfcsqk ywHwCltfcsqk = new YwHwCltfcsqk();
			
			ywHwCltfcsqk.setYwid(ywid);
			ywHwCltfcsqk.setQyid(qybh);
			ywHwCltfcsqk.setBgdd(bgdd);
			ywHwCltfcsqk.setAzjly(azjly);
			ywHwCltfcsqk.setBgcdmj(bgcdmj);
			ywHwCltfcsqk.setBgdxz(bgdxz);
			ywHwCltfcsqk.setCltfdd(cltfdd);
			ywHwCltfcsqk.setMbqk(mbqk);
			ywHwCltfcsqk.setSjgsqksm(sjgsqksm);
			ywHwCltfcsqk.setTfcdmj(tfcdmj);
			ywHwCltfcsqk.setSsqksm(ssqksm);
			ywHwCltfcsqk.setCltfdxz(cltfdxz);
			ywHwCltfcsqk.setTplcode(templateCode);
			this.SaveOrUpdate(ywHwCltfcsqk);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
